
<%@page import="org.apache.http.impl.client.BasicResponseHandler"%>
<%@page import="org.apache.http.client.ResponseHandler"%>
<%@page import="org.apache.http.client.methods.HttpGet"%>
<%@page import="org.apache.http.impl.client.DefaultHttpClient"%>
<%@page import="org.apache.http.client.HttpClient"%><%@page import="org.htmlparser.Node"%>
<%@page import="org.htmlparser.visitors.TagFindingVisitor"%>
<%@page import="org.htmlparser.tags.BaseHrefTag"%>
<%@page import="org.htmlparser.util.NodeList"%>
<%@page import="org.htmlparser.Parser"%>
<%@page import="java.net.URL"%>
<%@page import="java.net.HttpURLConnection"%>
<%
URL url = new URL(request.getParameter("url"));

/*
Parser.getConnectionManager().setRedirectionProcessingEnabled(true);
Parser p = new Parser(url.openConnection());
NodeList nl = p.parse(null);

BaseHrefTag baseTag = new BaseHrefTag();
baseTag.setBaseUrl(request.getParameter("url"));

TagFindingVisitor visitor = new TagFindingVisitor(new String[] {"HEAD"});
nl.visitAllNodesWith(visitor);
Node headNode = visitor.getTags(0)[0];
headNode.getChildren().prepend(baseTag);


out.write(nl.toHtml()+ "<script>parent.Ext.MessageBox.hide();</script>");
*/

HttpClient httpclient = new DefaultHttpClient();

HttpGet httpget = new HttpGet(request.getParameter("url")); 

StringBuffer responseBody;

try {
// Create a response handler
ResponseHandler<String> responseHandler = new BasicResponseHandler();
responseBody = new StringBuffer(httpclient.execute(httpget, responseHandler));

int index = (responseBody.indexOf("<head>") != -1) ? responseBody.indexOf("<head>"): (responseBody.indexOf("<HEAD>") != -1) ? responseBody.indexOf("<HEAD>") : -6; 
responseBody.insert(index+6, "\n<BASE href=\""+request.getParameter("url")+"\">\n");

if(null == request.getParameter("reload")) {
	responseBody.append("<script>parent.urlLoaded();</script>");
} else if (request.getParameter("reload").equals("true")) {
	responseBody.append("<script>parent.proceedTests();</script>");
}

}catch(Exception e){
	responseBody = new StringBuffer();
	responseBody.append("<script>parent.log.error('An exception has occurred on the server while loading the url.');"+
			"parent.showErrorBox('Exception', 'There was an exception while trying to load the url.<br><b>"+
			e+"</b>');</script>");
}

out.write(responseBody.toString());
%>